package cn.agiledata.bank.account.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.agiledata.bank.account.service.IAccountService;
import cn.agiledata.bank.common.util.WeatherUtil;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class CheckSessionFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)resp;
		IAccountService as = (IAccountService)IPBAppFactory.getObj("accountService");
		String url = request.getRequestURI();
		String paramter = request.getParameter("modual");
		if (StringUtils.isNotBlank(paramter) && paramter.equals("manage$query")) {
			request.getSession().setAttribute("requestURL", url + "?modual=" + paramter);
		}
		
		Long accountId = new Long(WeatherUtil.getAccountIdFromSession(request));
		
		if(accountId.longValue()==0 || as.lockAccount(accountId)){
			String uri = request.getRequestURI();
			if(uri.split("route.d").length>1) {
				String path=request.getContextPath();
				response.sendRedirect(path+"/logout.do");
				return;
			}
		}
		chain.doFilter(req, resp);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
